<?php

namespace Nota\NotaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Nota\NotaBundle\Entity as Entity;
use Nota\NotaBundle\Form as Form;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
$notas = $em->getRepository('NotaNotaBundle:Nota')
               ->todos();
               return $this->render('NotaNotaBundle:Page:mostrarNotas.html.twig', array(
        'notas' => $notas)); 
     //    return $this->render('NotaNotaBundle:Page:index.html.twig');
    }
    
        public function acercaDeAction()
    {
         return $this->render('NotaNotaBundle:Page:acercaDe.html.twig');
    }
    
            public function nuevaNotaAction()
    {
           $enquiry = new Entity\Nota();
    $form = $this->createForm(new Form\NotaType(), $enquiry);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // realiza alguna acción, como enviar un correo electrónico
                $nota = $form->getData();
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($nota);
                $em->flush();

            // Redirige - Esto es importante para prevenir que el usuario
            // reenvíe el formulario si actualiza la página
            return $this->redirect($this->generateUrl('nota_nota_mostrarNotas'));
        }
    }

    return $this->render('NotaNotaBundle:Form:nuevaNota.html.twig', array(
        'form' => $form->createView()
    ));
    }
    
    
            public function mostrarNotasAction()
    {
$em = $this->getDoctrine()->getManager();
$notas = $em->getRepository('NotaNotaBundle:Nota')
               ->todos();
               return $this->render('NotaNotaBundle:Page:mostrarNotas.html.twig', array(
        'notas' => $notas)); 
    }
    
    
            public function buscarNotaAction()
    {
                $request = $this->getRequest();
                $texto=$request->get('search-text');
$em = $this->getDoctrine()->getManager();
$notas = $em->getRepository('NotaNotaBundle:Nota')
               ->buscar($texto);
               return $this->render('NotaNotaBundle:Page:mostrarNotas.html.twig', array('notas' => $notas)); 
    }    
    
    
            public function mostrarNotaAction($id)
    {
$em = $this->getDoctrine()->getManager();
$nota = $em->getRepository('NotaNotaBundle:Nota')
               ->buscarPorId($id);
               return $this->render('NotaNotaBundle:Page:mostrarNota.html.twig', array('nota' => $nota)); 
    }    
    
    
                public function nuevoContactoAction()
    {
           $enquiry = new Entity\Contacto();
    $form = $this->createForm(new Form\ContactoType(), $enquiry);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // realiza alguna acción, como enviar un correo electrónico
                $contacto = $form->getData();
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($contacto);
                $em->flush();

            // Redirige - Esto es importante para prevenir que el usuario
            // reenvíe el formulario si actualiza la página
            return $this->redirect($this->generateUrl('nota_nota_mostrarContactos'));
        }
    }

    return $this->render('NotaNotaBundle:Form:nuevoContacto.html.twig', array(
        'form' => $form->createView()
    ));
    }
    
                public function mostrarContactosAction()
    {
$em = $this->getDoctrine()->getManager();
$contactos = $em->getRepository('NotaNotaBundle:Contacto')
               ->todos();
               return $this->render('NotaNotaBundle:Page:mostrarContactos.html.twig', array(
        'contactos' => $contactos)); 
    }
    
    
    /**
     * Displays a form to edit an existing Post entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('NotaNotaBundle:Post')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('No se encuentra entidad Nota.');
        }

        $editForm = $this->createForm(new PostType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('NotaNotaBundle:Nota:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    
     /**
     * Edits an existing Post entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('NotaNotaBundle:Nota')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('No se encuentra entidad Nota.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new PostType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('nota_edit', array('id' => $id)));
        }

        return $this->render('NotaNotaBundle:Nota:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Post entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('NotaNotaBundle:Nota')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('No se encuentra entidad Nota.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('post'));
    }

    /**
     * Creates a form to delete a Post entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    
}
